package com.jse.service;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;

import javax.script.ScriptException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.Dao;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.lang.Streams;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.jse.ioc.Ioc;

import jdk.nashorn.api.scripting.NashornScriptEngine;

/**
 * NashornRun表达引擎执行类 mvc代理服务组件
 * 
 * @author dzh
 * 
 */
public class NashornService {

	Log log = Logs.get();

	// @Inject RedisService redisService;//未使用pipeline
	// @Inject JedisPool jedisPool;//原生操作redis

	public Object run(String jjs, String func, NutMap tbl, HttpServletRequest req, HttpServletResponse resp,Ioc ioc)
			 {
		Dao dao =ioc.get("dao",Dao.class);
		CacheService cache=ioc.get("cache", CacheService.class);
		NashornScriptEngine scriptEngine=ioc.get("scriptEngine", NashornScriptEngine.class);
		PropertiesProxy conf=ioc.get("conf", PropertiesProxy.class);
		String jjs_path = this.getClass().getResource("/").getPath().substring(1);// .replace("/", "\\");
		boolean notExists = true;
		if (!Strings.isEmpty(jjs)) {
			String jjsaction = jjs + "\\" + func;
			if (Files.exists(Paths.get(jjs_path, jjsaction + ".jjs"), LinkOption.NOFOLLOW_LINKS)) {// 直接映射不存在
				jjs = jjsaction;
				notExists = false;
			} else if (func.contains("_")&&Files.exists(Paths.get(jjs_path, jjs+"\\"+func.substring(0, func.indexOf('_')) + ".jjs"), LinkOption.NOFOLLOW_LINKS)) {
				jjs = jjs + "\\" + func.substring(0, func.indexOf('_'));
				notExists = false;
			} else if (Files.exists(Paths.get(jjs_path, jjs + "/_.jjs"), LinkOption.NOFOLLOW_LINKS)) {
				jjs = jjs + "/_";
				notExists = false;
			}
		} else {
			jjs="index";
			if (Files.exists(Paths.get(jjs_path, func + ".jjs"), LinkOption.NOFOLLOW_LINKS)) {// 直接映射不存在
				notExists = false;
			}else if (Files.exists(Paths.get(jjs_path,"index.jjs"), LinkOption.NOFOLLOW_LINKS)) {// 直接映射不存在
				notExists = false;
			}
		}
		if (notExists) {
			log.warn(String.format("jjs: path %s/%s not jjs exists!", jjs,func));
			return null;
		}
		scriptEngine.put("req", req);
		scriptEngine.put("resp", resp);
		scriptEngine.put("session", req.getSession());
		scriptEngine.put("app", req.getServletContext());
		scriptEngine.put("conf", conf);
		scriptEngine.put("ioc", ioc);
		scriptEngine.put("dao", dao);
		scriptEngine.put("log", log);
		scriptEngine.put("cache", cache);
		scriptEngine.put("jjs", jjs_path);
			try {
				scriptEngine.eval(new InputStreamReader(this.getClass().getResourceAsStream("/jse.jjs")));
				scriptEngine.put("p", tbl);
				scriptEngine.eval(new String(Files.readAllBytes(Paths.get(jjs_path + jjs + ".jjs"))));
				if(Files.exists(Paths.get(jjs_path + "_.jjs"), LinkOption.NOFOLLOW_LINKS))
				scriptEngine.eval(new String(Files.readAllBytes(Paths.get(jjs_path  + "_.jjs"))));
				return scriptEngine.invokeFunction(func, tbl);
			} catch (ScriptException | IOException | NoSuchMethodException e) {
				return e;
			}
			
		
	}

	public Object run(String jjs, String func, Object tbl,Ioc ioc) throws ScriptException, NoSuchMethodException {
		String jjs_path = this.getClass().getResource("/").getPath().substring(1);// .replace("/", "\\");
		Dao dao =ioc.get("dao",Dao.class);
		PropertiesProxy conf=ioc.get("conf", PropertiesProxy.class);
		NashornScriptEngine scriptEngine=ioc.get("scriptEngine", NashornScriptEngine.class);
		CacheService cache=ioc.get("cache", CacheService.class);
		scriptEngine.put("conf", conf);
		scriptEngine.put("dao", dao);
		scriptEngine.put("log", log);
		scriptEngine.put("cache", cache);
		scriptEngine.put("jjs", jjs_path);
		scriptEngine.put("p", tbl);
		scriptEngine.eval(Streams.fileInr(jjs_path + jjs + ".jjs"));
		return scriptEngine.invokeFunction(func, tbl);
	}

	public static void main(String[] args) throws NoSuchMethodException, ScriptException {
		
	}
}
